package com.creative.util;

import android.content.Context;
import android.content.SharedPreferences;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by yinjialiang on 15-6-17.
 */
public class DataUtil {
    private final static String TAG = "DataUtil";

    public static String readString(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(key, 0);
        return sp.getString(key, null);
    }

    public static String readString(Context context, String key, String defaultValue) {
        SharedPreferences sp = context.getSharedPreferences(key, 0);
        return sp.getString(key, defaultValue);
    }

    public static void writeString(Context context, String key, String value) {
        SharedPreferences sp = context.getSharedPreferences(key, 0);
        sp.edit().putString(key, value).commit();
    }

    // 读取
    public static boolean readBoolean(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(key, 0);
        return sp.getBoolean(key, true);
    }

    // 存储
    public static void writeBoolean(Context context, String key, Boolean value) {
        SharedPreferences sp = context.getSharedPreferences(key, 0);
        SharedPreferences.Editor edit = sp.edit();
        edit.putBoolean(key, value);
        edit.commit();
    }

    // 读取
    public static int readInt(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(key, 0);
        return sp.getInt(key, 0);
    }

    // 存储
    public static void writeInt(Context context, String key, int value) {
        SharedPreferences sp = context.getSharedPreferences(key, 0);
        SharedPreferences.Editor edit = sp.edit();
        edit.putInt(key, value);
        edit.commit();
    }

    // 读取
    public static long readLong(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(key, 0);
        return sp.getLong(key, 0l);
    }

    // 存储
    public static void writeLong(Context context, String key, long value) {
        SharedPreferences sp = context.getSharedPreferences(key, 0);
        SharedPreferences.Editor edit = sp.edit();
        edit.putLong(key, value);
        edit.commit();
    }

    public static void remove(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(key, 0);
        sp.edit().remove(key).commit();
    }

    /**
     * put string preferences
     *
     * @param context
     * @param key     The name of the preference to modify
     * @param value   The new value for the preference
     * @return True if the new values were successfully written to persistent storage.
     */
    public static boolean putString(Context context, String preferenceName, String key, String value) {
        SharedPreferences settings = context.getSharedPreferences(preferenceName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(key, value);
        return editor.commit();
    }

    /**
     * put string preferences
     *
     * @param context
     * @param maps
     * @return True if the new values were successfully written to persistent storage.
     */
    public static boolean putMaps(Context context, String preferenceName, Map maps) {
        SharedPreferences settings = context.getSharedPreferences(preferenceName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();

        Iterator iterator = maps.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            String key = (String) entry.getKey();
            String val = (String) entry.getValue();
            editor.putString(key, val);
        }
        return editor.commit();
    }

    /**
     * get string preferences
     *
     * @param context
     * @param key     The name of the preference to retrieve
     * @return The preference value if it exists, or null. Throws ClassCastException if there is a preference with this
     * name that is not a string
     * @see #getString(android.content.Context, String, String)
     */
    public static String getString(Context context, String preferenceName, String key) {
        return getString(context, preferenceName, key, null);
    }

    /**
     * get string preferences
     *
     * @param context
     * @param key          The name of the preference to retrieve
     * @param defaultValue Value to return if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
     * this name that is not a string
     */
    public static String getString(Context context, String preferenceName, String key, String defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(preferenceName, Context.MODE_PRIVATE);
        return settings.getString(key, defaultValue);
    }


    /**
     *   get current time
     *
     */

    public static String getCurrentTime(String time) throws ParseException {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = df.parse(time);


        DateFormat chformat = new SimpleDateFormat("yyyy年MM月dd日");

        return chformat.format(date);
    }
}
